﻿module Sustainalytics.Home {

    import Common = Sustainalytics.Common;
    //import Insights = Sustainalytics.Insights;

    export class HomeService implements IHomeService {
        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private $location: ng.ILocationService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjects: Common.IStaticObjects;

        //Ctor.
        constructor($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: Common.INotificationFactory, staticObjects: Common.IStaticObjects) {
            this.$q = $q;
            this.$http = $http;
            this.$location = $location;
            this.notificationFactory = notificationFactory;
            this.staticObjects = staticObjects;
        }

        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjects.getErrorMesageByStatusCode(err, err['code']));
        }

        private showMessage(msg) {
            var ctx = this;
            ctx.notificationFactory.success(msg);
        }

        private eventsUrl(): string {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + this.staticObjects.eventsServiceBaseUrl
                + "/api/v1/events";
        }

        public getNewsTop(skip: number, take: number, orderBy: string[], orderAscending: boolean, accountId: string) {
            var ctx = this;
            var def = this.$q.defer();

            var options = new Common.ListOptions();
            options.skip = skip;
            options.take = take;
            options.orderBy = orderBy;
            options.orderAscending = orderAscending;
            options.includeAllFields = false;

            var query =
                {
                    term: null,
                    category: null,
                    //availableFilter: 1, //1 - All, 2 - Client, 3 - Prospect
                    accountId: accountId,
                    listOptions: options
                };

            var url = '/newsserv/api/v1/news?' + $.param(query);
            var promise = this.$http.get(url);
            promise
                .success(function (data) {
                    def.resolve(data);
                })
                .error(function (err) {
                    ctx.showError(err);
                    def.reject();
                })

            return def.promise;
        }

        public getEventsTop(skip: number, take: number, orderBy: string[], orderAscending: boolean) {
            var ctx = this;
            var def = this.$q.defer();

            var query =
                {
                    term: null,
                    eventType: null,
                    skip: skip,
                    take: take,
                    includeAllFields: false,
                    getCount: false,
                    orderBy: orderBy,
                    orderAscending: orderAscending,
                    isPublished: true
                };

            var url = ctx.eventsUrl() + "?" + $.param(query);
            var promise = this.$http.get(url);
            promise
                .success(function (data) {
                    def.resolve(data);
                })
                .error(function (err) {
                    ctx.showError(err);
                    def.reject();
                })

            return def.promise;
        }

        public getInsightsTop(skip: number, take: number, orderBy: string[], orderAscending: boolean) {

            var ctx = this;
            var def = this.$q.defer();

            var options = new Common.ListOptions();
            options.skip = skip;
            options.take = take;
            options.orderBy = orderBy;
            options.orderAscending = orderAscending;
            options.includeAllFields = false;

            var flt = new Insights.InsightFilterDto();
            flt.status = 1; //0 - Draft, 1 - Published, 3 - UnPublished

            var query =
                {
                    options: options,
                    filter: flt
                };

            var url = '/insightsserv/api/v1/insights?' + $.param(query);
            var promise = this.$http.get(url);
            promise
                .success(function (data) {
                    def.resolve(data);
                })
                .error(function (err) {
                    ctx.showError(err);
                    def.reject();
                })

            return def.promise;            
            
        }

        public getPortfoliosTop(skip: number, take: number, orderBy: string[], orderAscending: boolean, accountId: string, userId: string) {
            var ctx = this;
            var def = this.$q.defer();

            var options = new Common.ListOptions();
            options.skip = 0;
            options.take = 5;
            options.orderBy = orderBy;
            options.orderAscending = orderAscending;
            options.includeAllFields = false;
            options.getCount = false;

            var params = $.param({
                isMyPortfolio: true,
                namePortfolio: null,
                sharingStatus: null,
                isNameEqual: false,
                accountId: accountId,
                userId: userId,
                listOptions: options
            });

            var url = '/portfolioserv/api/v1/portfolios?' + params;
            var promise = this.$http.get(url);
            promise
                .success(function (data) {
                    def.resolve(data);
                })
                .error(function (err) {
                    ctx.showError(err);
                    def.reject();
                })

            return def.promise;
        }
    }

    // register service with Home Angular module
    angular.module('Sustainalytics.Home').factory('HomeService', function ($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: Common.INotificationFactory, staticObjects: Common.IStaticObjects) {
        var srv = new HomeService($q, $http, $location, notificationFactory, staticObjects);
        return srv;
    });
}